package com.zprmb.udzt.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshExpandableListView;
import com.zprmb.udzt.R;
import com.zprmb.udzt.V6DetailsActivity;
import com.zprmb.udzt.adapter.ExpandableBaseAdapter;
import com.zprmb.udzt.bean.ExpandableBean;
import com.zprmb.udzt.bean.Product_Uplan;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.ToJSON;
import com.zprmb.udzt.util.VolleryUtil;
import com.zprmb.udzt.util.VolleryUtil3;

import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import static com.zprmb.udzt.R.id.iv_v4_nonetwork;
//import com.zprmb.udzt.adapter.ExpandableBaseAdapter.Help;

public class ExpandableFragment extends BaseFragment implements OnRefreshListener2<ExpandableListView>{
	private PullToRefreshExpandableListView expandableListView;
	private ExpandableBaseAdapter adapter;
	private int scrolledX,scrolledY;
	private List<ExpandableBean> list=new ArrayList<ExpandableBean>();
	private List<Product_Uplan> list2=new ArrayList<Product_Uplan>();
	private ImageView img_nowork;
	private List<String> group;
	private List<List<String>> child;
	DecimalFormat df = new DecimalFormat("###0.00");  
	 DecimalFormat df2 = new DecimalFormat("###0.0");
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view=inflater.inflate(R.layout.product_uplan_two, null);

//		initData();
		img_nowork=(ImageView)view.findViewById(iv_v4_nonetwork);
		expandableListView=(PullToRefreshExpandableListView) view.findViewById(R.id.expandableListView);



//		initData(true);
		expandableListView.setOnRefreshListener(this);
		expandableListView.getRefreshableView().setGroupIndicator(null);
		expandableListView.getRefreshableView().setChildIndicator(null);
		Drawable drawable=getActivity().getResources().getDrawable(R.color.black);
		expandableListView.getRefreshableView().setChildDivider(drawable);
		expandableListView.getLoadingLayoutProxy().setPullLabel("下拉可刷新");
		expandableListView.getLoadingLayoutProxy().setRefreshingLabel("智慧投资 更优一点");
		expandableListView.getLoadingLayoutProxy().setReleaseLabel("松开即刷新");
//		expandableListView.getRefreshableView().addFooterView(getFooter(),null,false);
		expandableListView.getRefreshableView().setOnGroupClickListener(new OnGroupClickListener() {
			
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				// TODO Auto-generated method stub
				return true;
			}
		});

	expandableListView.getRefreshableView().setOnChildClickListener(new OnChildClickListener() {
		
		@Override
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {
			// TODO Auto-generated method stub
//			Intent intent=new Intent(getActivity(),V6DetailsActivity.class);
			Intent intent=new Intent(getActivity(),V6DetailsActivity.class);
			intent.putExtra("name", list.get(groupPosition).getList().get(childPosition).getProductName());
			intent.putExtra("type", list.get(groupPosition).getList().get(childPosition).getProductType());							
			intent.putExtra("id", list.get(groupPosition).getList().get(childPosition).getLoanId());
			startActivityForResult(intent, 1);
			return false;
		}
	});

		img_nowork.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				initData(true);
			}
		});
		if (!network()) {
			expandableListView.setVisibility(View.GONE);
			img_nowork.setVisibility(View.VISIBLE);
		} else {
			expandableListView.setVisibility(View.VISIBLE);
			img_nowork.setVisibility(View.GONE);
			initData(true);
		}
		return view;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
//		initData(false);
		if (!network()) {
			expandableListView.setVisibility(View.GONE);
			img_nowork.setVisibility(View.VISIBLE);
		}else if (list.isEmpty()){
			initData(true);
		}else {

		}


	}
	private void initData2(boolean isShow) {

		VolleryUtil3 volleryUtil = new VolleryUtil3() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				LogUtils.e(getActivity(), jsonObject.toString());
				ToJSON.initData2(jsonObject.toString(), "xindingqi");
				expandableListView.setVisibility(View.VISIBLE);
				img_nowork.setVisibility(View.GONE);
				Gson gson = new Gson();
				if (!jsonObject.optString("data").equals("null")) {
					list = gson.fromJson(jsonObject.optString("data"),
							new TypeToken<ArrayList<ExpandableBean>>() {
							}.getType());
				}

				if(list.get(0)==null||list.get(0).getList()==null){
					expandableListView.setVisibility(View.GONE);
					img_nowork.setVisibility(View.VISIBLE);
				}else{

					if(list.get(0).getList().size()>=1){

						adapter = new ExpandableBaseAdapter(getActivity(), list);
						expandableListView.getRefreshableView().setAdapter(adapter);
						adapter.notifyDataSetChanged();

						for(int i=0;i<list.size();i++){
							expandableListView.getRefreshableView().expandGroup(i);
						}
					}
				}


			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub
				expandableListView.setVisibility(View.GONE);
				img_nowork.setVisibility(View.VISIBLE);
				Toast.makeText(getActivity(), "请求失败   请检查网络设置", 2000).show();
			}
		};
		volleryUtil.setShow(isShow);
		volleryUtil.setRequestMethod(0);
//		volleryUtil.execute(getActivity(), Constant.URL
//				+ "api/v5/list/uplanList", "");
		volleryUtil.execute(getActivity(), Constant.URL
				+ "api/v6/render/productList", "");
//		Log.e("Uplan", Constant.URL + "api/v5/list/uplanList");

	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		initData(false);
	}

	private void initData(boolean isShow) {

		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				LogUtils.e(getActivity(), jsonObject.toString());
				ToJSON.initData2(jsonObject.toString(), "xindingqi");
				expandableListView.setVisibility(View.VISIBLE);
				img_nowork.setVisibility(View.GONE);
				Gson gson = new Gson();
				if (!jsonObject.optString("data").equals("null")) {
					list = gson.fromJson(jsonObject.optString("data"),
							new TypeToken<ArrayList<ExpandableBean>>() {
							}.getType());
				}

				if(list.get(0)==null||list.get(0).getList()==null){
					expandableListView.setVisibility(View.GONE);
					img_nowork.setVisibility(View.VISIBLE);
				}else{
					
					if(list.get(0).getList().size()>=1){
						
						adapter = new ExpandableBaseAdapter(getActivity(), list);
						expandableListView.getRefreshableView().setAdapter(adapter);
						adapter.notifyDataSetChanged();
						
					for(int i=0;i<list.size();i++){
						expandableListView.getRefreshableView().expandGroup(i);
					}
					}
				}


			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub
				expandableListView.setVisibility(View.GONE);
				img_nowork.setVisibility(View.VISIBLE);
				Toast.makeText(getActivity(), "请求失败   请检查网络设置", 2000).show();
			}
		};
		volleryUtil.setShow(isShow);
		volleryUtil.setRequestMethod(0);
//		volleryUtil.execute(getActivity(), Constant.URL
//				+ "api/v5/list/uplanList", "");
				volleryUtil.execute(getActivity(), Constant.URL
						+ "api/v6/render/productList", "");
//		Log.e("Uplan", Constant.URL + "api/v5/list/uplanList");

	}
	public static ExpandableFragment newInstance(int position, String string) {
		ExpandableFragment uf = new ExpandableFragment();
		return uf;
	}
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (!network()) {
				expandableListView.setVisibility(View.GONE);
				img_nowork.setVisibility(View.VISIBLE);
			} else {
				expandableListView.setVisibility(View.VISIBLE);
				img_nowork.setVisibility(View.GONE);
				list.clear();
				initData2(true);
			}


					expandableListView.onRefreshComplete();
//				}
//			}, 600);
//			
		};
	};
	@Override
	public void onPullDownToRefresh(
			PullToRefreshBase<ExpandableListView> refreshView) {
		// TODO Auto-generated method stub
		handler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				handler.sendEmptyMessage(1);
			}
		}, 2000);
	
	}
	@Override
	public void onPullUpToRefresh(
			PullToRefreshBase<ExpandableListView> refreshView) {
		// TODO Auto-generated method stub
		
	}

}
